// 登录相关
import { userLogin, userLogOut, getAllUser, updateUserState, deleteUser, register } from '@/api/user'
export default {
    state: {
        userInfo: JSON.parse(localStorage.getItem('userInfo')) || {},
        token: localStorage.getItem('TOKEN') || '',
        userList: [],
    },
    actions: {
        // 登录
        async toUserLogin({ commit }, userData) {
            let result = await userLogin(userData);
            if (result.code === 200) {//登录成功
                commit('setToken', result.data);
                localStorage.setItem('TOKEN', result.data.token);
                return 'success'
            } else {//登录失败
                return Promise.reject(new Error('failed'))
            }
        },
        // 退出登录
        async toUserLogOut({ commit }) {
            let result = await userLogOut();
            if (result.code == 200) {
                commit('setLogout');
                return 'ok'
            } else {
                return Promise.reject(new Error('failed'));
            }
        },
        // 获取管理员列表
        toGetAllUser({ commit }) {
            getAllUser().then(res => {
                commit('setAllUser', res)
            }).catch(err => {
                console.log(err);
            })
        },
        // 更新管理员状态
        async toUpdateUserState(context, info) {
            let result = await updateUserState(info);
            if (result.code === 200) {
                context.dispatch('toGetAllUser');
                return 'ok'
            } else {
                return Promise.reject(new Error('failed'))
            }
        },
        // 删除管理员
        async toDeleteUser(context, id) {
            let result = await deleteUser(id);
            if (result.code === 200) {
                context.dispatch('toGetAllUser');
                return 'ok';
            } else {
                return Promise.reject(new Error('failed'))
            }
        },
        // 添加管理员
        async toRegister(context, userData) {
            let result = await register(userData);
            if (result === 'register success') {
                context.dispatch('toGetAllUser');
                return 'ok'
            } else {
                return Promise.reject(new Error('failed'))
            }
        }
    },
    mutations: {
        // 储存token与用户信息
        setToken(state, data) {
            let { username, email, status } = data.user
            state.userInfo = { username, email, status };
            localStorage.setItem('userInfo', JSON.stringify({ username, email, status }));
            console.log('存token值');
            state.token = data.token;
        },
        // 退出登录/清空token
        setLogout(state) {
            state.userInfo = {};
            state.token = '';
            localStorage.removeItem('TOKEN');
            localStorage.removeItem('userInfo');
        },
        // 处理并储存管理员列表
        setAllUser(state, userList) {
            userList.map(item => {
                delete item.password;
                item.state = item.state === 'always' ? '正常' : '停用';
                item.status = item.status === "super" ? '超级管理员' : '管理员';
            });
            state.userList = userList;
        }
    },
    getters: {}
}